package ngat.oss.simulation.test;

import java.util.*;
import java.rmi.*;
import ngat.util.*;
import ngat.phase2.*;
import ngat.oss.simulation.*;

/** Load the phase2model from a provider and check its content.*/
public class LoadPhase2Test2 {

    public static void main(String[] args) {

	try {
	    
	    ConfigurationProperties cfg = CommandTokenizer.use("--").parse(args);
	    
	    String root = cfg.getProperty("root");
	    
	    int count = cfg.getIntValue("repeat", 1);

	    // times:
	    // t1 - t0 = registry lookup
	    // t2 - t1 = fetch
	    // t3 - t2 = scan
	    
	    for (int run = 0; run < count; run++) {

		long t0 = System.currentTimeMillis();
		
		Phase2ModelProvider p2p = (Phase2ModelProvider)Naming.lookup(root+"_Phase2ModelProvider");
		long t1 = System.currentTimeMillis();
		
		
		Phase2Model model = p2p.getPhase2Model();
		long t2 = System.currentTimeMillis();
		
		int np = 0;
		int na = 0;
		int ng = 0;
		int no = 0;
		long now = System.currentTimeMillis();
		Iterator ip = model.listProposals();
		while (ip.hasNext()) {
		    Proposal proposal = (Proposal)ip.next();
		    //System.err.println("Proposal: "+proposal.getFullPath());
		    np++;
		
		    if (proposal.getActivationDate() < now && proposal.getExpiryDate() > now) { 
			na++;
			Iterator ig = proposal.listAllGroups();
			while (ig.hasNext()) {
			    Group group = (Group)ig.next();
			    ng++;
			    Iterator io = group.listAllObservations();
			    while (io.hasNext()) {
				Observation obs = (Observation)io.next();
				no++;
			    }
			}
		    }
		    
		}
		long t3 = System.currentTimeMillis();
		//System.err.println("Phase2: "+root+
		// " contains "+np+" proposals of which "+na+" are active "+
		// " and "+ng+" groups with "+no+" observations");
		
		System.err.println("Run: "+run+" Lookup: "+(t1-t0)+" Fetch: "+(t2-t1)+" Scan: "+(t3-t2));

	    }
	    
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
    
}
